package com.zhipu.balance.utils.upload.rice

import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.zhipu.balance.listener.ChooseClickCallback
import com.zhipu.balance.mobile.ResultEntity
import com.zhipu.balance.mobile.event.RecordRice
import com.zhipu.balance.remotedata.InterceptObserver
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.dialog.CommonDialog
import com.zhy.http.okhttp.OkHttpUtils
import com.zhy.http.okhttp.callback.StringCallback
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.Call
import okhttp3.MediaType

/**
 * 稻谷上传
 */
object RiceUploadUtils {

    /**
     * 外网上传稻谷天平数据
     */
    fun uploadRiceBalanceInfo(mRecordRice : RecordRice,mChooseClickCallback : ChooseClickCallback?){
        var mNowMills = TimeUtils.getNowMills()
        var mGson = GsonUtils.toJson("${mRecordRice}&${mNowMills}")
        var byteSign = InitializeUtils.getSignV0()?.signatureParams(mGson)
        var signature = ConvertUtils.bytes2HexString(byteSign)
        var mObservable = InitializeUtils.AppComponent()?.systemService?.uploadRiceBalanceInfo(signature,mNowMills,mRecordRice)
        mObservable?.subscribeOn(Schedulers.io())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : InterceptObserver<String>() {
            /**
             * 成功数据data
             */
            override fun <T> onNextData(resultEntity: ResultEntity<T>?) {
                InitializeUtils.getDefaultMaker()?.show(resultEntity?.msg)
                mChooseClickCallback?.onChooseClickCallback(0)
                /**
                 * 关闭
                 */
                CommonDialog.dismissGP()
            }

            /**
             * 错误网络
             */
            override fun onErrorNet(t: Throwable?, netMsg: String?) {
                InitializeUtils.getDefaultMaker()?.show("上传失败")
                mChooseClickCallback?.onChooseClickCallback(1)
                /**
                 * 关闭
                 */
                CommonDialog.dismissGP()
            }

        })
    }


    /**
     * 内网上传稻谷天平数据
     */
    fun IntranetRiceBalanceInfo(mRecordRice : RecordRice,mChooseClickCallback : ChooseClickCallback?){
        OkHttpUtils.postString()
            .url("http://"+ SPUtils.getInstance().getString("NetworkSettings","https://cg.zhipu-china.com/") + "/uploadRiceBalanceInfo")
            .content(GsonUtils.toJson(mRecordRice))
            .mediaType(MediaType.parse("application/json; charset=utf-8")).build().execute(object : StringCallback() {
                override fun onError(call: Call?, e: Exception?, id: Int) {
                    InitializeUtils.getDefaultMaker()?.show("上传失败")
                    mChooseClickCallback?.onChooseClickCallback(1)
                    /**
                     * 关闭
                     */
                    CommonDialog.dismissGP()
                }
                override fun onResponse(response: String?, id: Int) {
                    var mResultEntity = GsonUtils.fromJson(response,ResultEntity::class.java)
                    when(mResultEntity.status){
                        200 ->{
                            InitializeUtils.getDefaultMaker()?.show(mResultEntity.msg)
                            mChooseClickCallback?.onChooseClickCallback(0)
                        }
                        else ->{
                            InitializeUtils.getDefaultMaker()?.show("上传失败")
                            mChooseClickCallback?.onChooseClickCallback(1)
                        }
                    }
                    /**
                     * 关闭
                     */
                    CommonDialog.dismissGP()
                }
            })
    }
}